package com.example.testlxr9096.adapter;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.testlxr9096.R;
import com.example.testlxr9096.bean.ImageItem;

import java.util.List;

public class WeiQianImageAdapter extends RecyclerView.Adapter<WeiQianImageAdapter.ViewHolder> {

    private List<ImageItem> ImageItems;
    private int lastSelectedPosition = -1;

    private OnImageItemClickListener listener;

    public static int clickPosition = -1;

    public interface OnImageItemClickListener {
        void onImageItemClick(int position);
    }
    public void setOnImageItemClickListener(OnImageItemClickListener listener) {
        this.listener = listener;
    }



    public WeiQianImageAdapter(List<ImageItem> ImageItems) {
        this.ImageItems = ImageItems;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.weiqian__imageitem, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        ImageItem ImageItem = ImageItems.get(position);

        // 设置文本
        holder.title.setText(ImageItem.getName());
        switch (ImageItem.getState()){
            case 0 :
                holder.imageView.setImageResource(ImageItem.getImage());

                break;
            case 1 :
                holder.imageView.setImageResource(ImageItem.getImage_pass());
                break;
            case 2 :
                holder.imageView.setImageResource(ImageItem.getImage_ng());
                break;

        }



        // 设置背景

        // 点击事件
        holder.imageView.setOnClickListener(v -> {
            clickPosition = position;

            Log.d("readTaskposition","位置 +"+position);
            if (lastSelectedPosition != position) {
                // 取消上一次选中
                if (lastSelectedPosition != -1) {

                    notifyItemChanged(lastSelectedPosition);
                }

                // 设置当前选中

                notifyItemChanged(lastSelectedPosition);
                lastSelectedPosition = position;
            }
            int pos = holder.getAdapterPosition();
            if (pos != RecyclerView.NO_POSITION) {
                if (listener != null) {
                    listener.onImageItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
          return ImageItems.size();
    }




    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        ImageView imageView;

        public ViewHolder(View ImageItemView) {
            super(ImageItemView);
            title = ImageItemView.findViewById(R.id.tv_type);
            imageView = ImageItemView.findViewById(R.id.iv_iamge);
        }
    }
}